package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import com.zhijian.medical.dao.local.PersonProfessionalEvaluationMapper;
import com.zhijian.medical.entity.po.PersonProfessionalEvaluation;
import com.zhijian.medical.entity.vo.request.PersonProfessionalEvaluationSearchVo;
import com.zhijian.medical.entity.vo.response.PersonProfessionalEvaluationVo;
import com.zhijian.medical.service.PersonProfessionalEvaluationService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员-专业评估业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-08-11 15:12:37
 */
@Slf4j
@Service
public class PersonProfessionalEvaluationServiceImpl implements PersonProfessionalEvaluationService {

    @Autowired
    private PersonProfessionalEvaluationMapper mapper;

    @Override
    public PersonProfessionalEvaluation selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonProfessionalEvaluationVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonProfessionalEvaluation personProfessionalEvaluation) {
        Date now = new Date();
        personProfessionalEvaluation.setCreateTime(now);
        personProfessionalEvaluation.setUpdateTime(now);
        return mapper.insert(personProfessionalEvaluation) > 0 ? personProfessionalEvaluation.getId() : 0L;
    }

    @Override
    public boolean update(PersonProfessionalEvaluation personProfessionalEvaluation) {
        Date now = new Date();
        personProfessionalEvaluation.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personProfessionalEvaluation) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(PersonProfessionalEvaluation record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(PersonProfessionalEvaluationSearchVo personProfessionalEvaluationSearchVo) {
        return mapper.count(personProfessionalEvaluationSearchVo);
    }

    @Override
    public List<PersonProfessionalEvaluationVo> selectList(PersonProfessionalEvaluationSearchVo personProfessionalEvaluationSearchVo) {
        return mapper.selectList(personProfessionalEvaluationSearchVo);
    }

    @Override
    public Page<PersonProfessionalEvaluationVo> selectPage(PersonProfessionalEvaluationSearchVo personProfessionalEvaluationSearchVo) {
        //先直接查总数
        int count = count(personProfessionalEvaluationSearchVo);
        //分页
        Page<PersonProfessionalEvaluationVo> page = new Page<>(true, count, personProfessionalEvaluationSearchVo.getPageNum(), personProfessionalEvaluationSearchVo.getPageSize());
        personProfessionalEvaluationSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonProfessionalEvaluationVo> responseVos = this.selectList(personProfessionalEvaluationSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<PersonProfessionalEvaluation> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    public static final String CODE_PREFIX = "BLK-PF-016-002";

    @Override
    public String selectMaxCode() {
        return mapper.selectMaxCodeByDay(DateUtil.formatDate(new Date()));
    }

    @Override
    public String createNextCode() {
        String maxCode = this.selectMaxCode();
        if (StringUtils.isBlank(maxCode)) {
            return String.format("%s-%s-1", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")));
        }
        String[] maxCodeArray = maxCode.split("-");
        Integer i = Integer.valueOf(maxCodeArray[maxCodeArray.length - 1]);
        i++;
        return String.format("%s-%s-%s", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")), i);
    }

}